package common.util;

//import com.yuntsg.intercepter.bean.User;
//import common.exceptions.UnloginException;

import common.beans.PermissionDto;
import common.beans.User;
import common.exceptions.UnloginException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.util.List;
import java.util.Locale;

/**
 * 用户工具类
 * 此处的内容
 */
@Slf4j
public class UserUtil {

    private final static ThreadLocal<User> tlUser = new ThreadLocal<>();

    private final static ThreadLocal<String> tlAccess = new ThreadLocal<>();

    private final static ThreadLocal<Locale> tlLocale = new ThreadLocal<Locale>() {
        protected Locale initialValue() {
            // 语言的默认值
            return Locale.CHINESE;
        }

        ;
    };

    public static final String KEY_LANG = "lang";

    public static final String KEY_USER = "user";

    public static void setAccessId(String accessId) {
        tlAccess.set(accessId);
    }

    /**
     * 如果没有登录，允许访问, 返回null
     *
     * @return Users
     */
    public static String getAccessId() {
        return tlAccess.get();
    }

    public static void setUser(User user) {
        tlUser.set(user);
        // 把用户信息放到log4j
        MDC.put(KEY_USER, user.getUid() + "");
    }

    /**
     * 如果没有登录，允许访问, 返回null
     *
     * @return Users
     */
    public static User getUserUnloginAccess() {
        return tlUser.get();
    }

    /**
     * 如果没有登录会抛出异常
     *
     * @return Integer
     */
    public static User getUserUnloginDeniey() {
        User user = tlUser.get();
        if (user == null) {
            //	throw new UnloginException();
            return null;
        }
        return user;
    }

    /**
     * 本方法仅用于需要获取单位gid,但是未登录也照样可以访问的情况,未登录状态默认null
     */
    public static Integer getUserGidUnloginAccess() {
        Integer gid = null;
        User user = getUserUnloginAccess();
        if (user != null) {
            gid = user.getGid();
        }
        return gid;
    }

    /**
     * 本方法必须要求用户登录,未登录则抛出未登录异常.
     */
    public static Integer getUserUid() {
        User user = getUserUnloginDeniey();
        if (null == user) {
            return 1;
        }
        return user.getUid();
    }

    /**
     * 本方法必须要求用户登录,未登录则抛出未登录异常.
     */
    public static Integer getUserGid() {
        User user = getUserUnloginDeniey();
        Integer gid = user.getGid();
        return gid;
    }


    /**
     * 开关型的权限判断
     */
    public static boolean isPermitted(String permissionName) {
        boolean isPermitted = checkPermission(permissionName, null);
        return isPermitted;
    }

    /**
     * 数值型的开关判断
     */
    public static boolean isPermitted(String permissionName, Integer value) {
        List<PermissionDto> permissionDtoList = getUserUnloginDeniey().getPermissionList();
        if (permissionDtoList == null) {
            log.info("没有权限信息!!!!!");
            return false;
        }
        return checkPermission(permissionName, value);
    }

    private static boolean checkPermission(String permissionName, Integer curNum) {
        List<PermissionDto> permissionDtoList = getUserUnloginDeniey().getPermissionList();
//		System.out.println("要确认的权限:"+permissionName);
        for (PermissionDto perm : permissionDtoList) {
//			System.out.println("存储的权限:"+perm.getName());
            if (perm.getName().equals(permissionName)) {
                if (curNum != null) {
                    int limitNum = perm.getValue();
                    if (limitNum <= curNum) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }

    public static PermissionDto getPermissionByName(String permissionName) {
        List<PermissionDto> permissionDtoList = getUserUnloginDeniey().getPermissionList();
        for (PermissionDto perm : permissionDtoList) {
            if (perm.getName().equals(permissionName)) {
                return perm;
            }
        }
        return null;
    }

    public static void clearAllUserInfo() {
        tlUser.remove();
        tlLocale.remove();
        tlAccess.remove();
        MDC.remove(KEY_USER);
    }

}
